package com.example.design.flow.test;

import com.example.design.flow.chain.FlowChain;
import com.example.design.flow.chain.FlowChainManager;
import com.example.design.flow.node.EmptySubFlowNode;
import com.example.design.flow.test.test01Normal.ChangeSimStatusByOperatorFlowNode;
import com.example.design.flow.test.test01Normal.CreateLedgerFlowNode;
import com.example.design.flow.test.test01Normal.CreateOrderFlowNode;
import com.example.design.flow.test.test02Exception.ThrowExceptionNode;
import com.example.design.flow.test.test03Skip.SkipNode;
import com.example.design.flow.test.test03Skip.UnreachableNode;
import com.example.design.flow.test.test04Sub.ChangePackageFlowNode;
import com.example.design.flow.test.test04Sub.TestSubFlowNode;

import java.lang.reflect.InvocationTargetException;

public class FlowDemo {

    public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
//        test01();
//        test02();
//        test03();
//        test04();
        test05();
    }



     // 测试正常流程执行
    private static void test01() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        FlowChainManager manager = new FlowChainManager();
        FlowChain<String> flowChain = FlowChain.newChain(manager, String.class);
        flowChain.addNode(CreateOrderFlowNode.class);
        flowChain.addNode(ChangeSimStatusByOperatorFlowNode.class);
        flowChain.addNode(CreateLedgerFlowNode.class);
        manager.execute(flowChain, "asd");
    }

    // 测试异常回滚流程
    private static void test02() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        FlowChainManager manager = new FlowChainManager();
        FlowChain<String> flowChain = FlowChain.newChain(manager, String.class);
        flowChain.addNode(CreateOrderFlowNode.class);
        flowChain.addNode(ChangeSimStatusByOperatorFlowNode.class);
        flowChain.addNode(CreateLedgerFlowNode.class);
        flowChain.addNode(ThrowExceptionNode.class);
        manager.execute(flowChain, "asd");
    }

    // 测试SkipException中断流程
    private static void test03() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        FlowChainManager manager = new FlowChainManager();
        FlowChain<String> flowChain = FlowChain.newChain(manager, String.class);
        flowChain.addNode(CreateOrderFlowNode.class);
        flowChain.addNode(ChangeSimStatusByOperatorFlowNode.class);
        flowChain.addNode(CreateLedgerFlowNode.class);
        flowChain.addNode(SkipNode.class);
        flowChain.addNode(UnreachableNode.class);
        manager.execute(flowChain, "asd");
    }

    // 测试子流程执行
    private static void test04() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        FlowChainManager manager = new FlowChainManager();

        // TestSubFlowNode节点的构造子链
        FlowChain<String> testSubFlowNodeSubFlowChain = FlowChain.newChain(manager, String.class);
        testSubFlowNodeSubFlowChain.addNode(CreateOrderFlowNode.class);
        testSubFlowNodeSubFlowChain.addNode(ChangeSimStatusByOperatorFlowNode.class);
        testSubFlowNodeSubFlowChain.addNode(CreateLedgerFlowNode.class);

        // 主链
        FlowChain<String> mainFlowChain = FlowChain.newChain(manager, String.class);
        TestSubFlowNode testSubFlowNode = new TestSubFlowNode();
        testSubFlowNode.setSubChain(testSubFlowNodeSubFlowChain);
        mainFlowChain.addNode(testSubFlowNode);
        mainFlowChain.addNode(ChangePackageFlowNode.class);

        manager.execute(mainFlowChain, "Asd");
    }

    // EmptySubFlowNode空节点测试
    private static void test05() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        FlowChainManager manager = new FlowChainManager();

        // TestSubFlowNode节点的构造子链
        FlowChain<String> testSubFlowNodeSubFlowChain = FlowChain.newChain(manager, String.class);
        testSubFlowNodeSubFlowChain.addNode(CreateOrderFlowNode.class);
        testSubFlowNodeSubFlowChain.addNode(ChangeSimStatusByOperatorFlowNode.class);
        testSubFlowNodeSubFlowChain.addNode(CreateLedgerFlowNode.class);

        // 主链
        FlowChain<String> mainFlowChain = FlowChain.newChain(manager, String.class);
        EmptySubFlowNode emptySubFlowNode = new EmptySubFlowNode();
        emptySubFlowNode.setSubChain(testSubFlowNodeSubFlowChain);
        mainFlowChain.addNode(emptySubFlowNode);
        mainFlowChain.addNode(ChangePackageFlowNode.class);

        manager.execute(mainFlowChain, "Asd");
    }

}
